package com.smfx.yxt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.enums.SendeeType;
import com.smfx.yxt.reqVo.NoticeQueryVO;
import com.smfx.yxt.service.IClassGroupChatService;
import com.smfx.yxt.service.INoticeService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.ClassChatReadView;
import com.smfx.yxt.view.TeacherInfoView;

@Controller
public class MessageController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(MessageController.class);
	
	@Autowired
	private INoticeService noticeService;
	@Autowired
	private IClassGroupChatService classGroupChatService;
	@Autowired
	private ITeacherService teacherService;

	/**
	 * 老师消息页
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.TEACHER_ROLE + "/message")
	public String teacherMessage(Model model) {
		AppUser appUser = getAppUser();
		NoticeQueryVO vo = new NoticeQueryVO();
		vo.setSendeeId(appUser.getTeacher().getId());
		vo.setSendeeType(SendeeType.TEACHER.getCode());
		model.addAttribute("notices", noticeService.selectCountForSendee(vo));
		return "message/teacherMessage";
	}
	
	/**
	 * 家长消息页
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.STUDENT_ROLE + "/message")
	public String studentMessage(Model model) {
		AppUser appUser = getAppUser();
		NoticeQueryVO vo = new NoticeQueryVO();
		vo.setSendeeId(appUser.getParent().getId());
		vo.setSendeeOpenid(appUser.getParent().getOpenid());
		vo.setSendeeType(SendeeType.PARENT.getCode());
		model.addAttribute("notices", noticeService.selectCountForSendee(vo));
		return "message/studentMessage";
	}
	
	/**
	 * 消息聊天数据查询接口
	 * @param reqList
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/message/chat")
	@ResponseBody Object messageChat(@RequestBody ClassChatReadView[] reqList) {
		try {
			AppUser appUser = getAppUser();
			Map<String, Object> map = new HashMap<>();
			List<TeacherInfoView> classList = teacherService.selectClazzListByOpenid(appUser.getOpenid());
			if(StringUtil.isNullOrEmpty(classList)) {
				return new ArrayList<>();
			}
			map.put("classList", classList);
			map.put("chatList", reqList);
			List<ClassChatReadView> resultList = classGroupChatService.selectClazzChatList(map);
			return resultList;
		} catch (Exception e) {
			logger.error("---messageChat error={}", e);
		}
		return null;
	}
	
}
